package com.example.mywechat;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.mywechat.adapter.MyFragmentPagerAdapter;
import com.example.mywechat.fragment.HomeFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ViewPager2 vpMain;

    private LinearLayout llHome, llPinglun, llFaxian, llMe;
    private ImageView ivHome, ivPinglun, ivFaxian, ivMe, ivCurrent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化ViewPager
        initPager();
        // 初始化底部导航
        initTabView();
    }

    private void initTabView() {
        llHome = findViewById(R.id.ll_tab_home);
        llHome.setOnClickListener(this);
        llPinglun = findViewById(R.id.ll_tab_pinglun);
        llPinglun.setOnClickListener(this);
        llFaxian = findViewById(R.id.ll_tab_faxian);
        llFaxian.setOnClickListener(this);
        llMe = findViewById(R.id.ll_tab_me);
        llMe.setOnClickListener(this);

        ivHome = findViewById(R.id.iv_tab_home);
        // 设置home默认选择
        ivHome.setSelected(true);
        ivCurrent = ivHome;
        ivPinglun = findViewById(R.id.iv_tab_pinglun);
        ivFaxian = findViewById(R.id.iv_tab_faxian);
        ivMe = findViewById(R.id.iv_tab_me);
    }

    /**
     * 初始化ViewPager
     */
    private void initPager() {
        vpMain = findViewById(R.id.vp_main);
        MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), getLifecycle());
        List<Fragment> fragmentList = new ArrayList<>(4);
        fragmentList.add(HomeFragment.newInstance("首页"));
        fragmentList.add(HomeFragment.newInstance("评论"));
        fragmentList.add(HomeFragment.newInstance("发现"));
        fragmentList.add(HomeFragment.newInstance("我的"));
        pagerAdapter.setFragmentList(fragmentList);
        vpMain.setAdapter(pagerAdapter);
        // 设置页面改变回调
        vpMain.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            /**
             * 添加动画效果
             * @param position Position index of the first page currently being displayed.
             *                 Page position+1 will be visible if positionOffset is nonzero.
             * @param positionOffset Value from [0, 1) indicating the offset from the page at position.
             * @param positionOffsetPixels Value in pixels indicating the offset from position.
             */
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                // 页面切换
                changeTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
    }

    /**
     * 页面切换，使Tab按钮选中
     * @param position 页面
     */
    private void changeTab(int position) {
        ivCurrent.setSelected(false);
        if (position == 0 || position == R.id.ll_tab_home) {
            vpMain.setCurrentItem(0);
            ivHome.setSelected(true);
            ivCurrent = ivHome;
        } else if (position == 1 || position == R.id.ll_tab_pinglun) {
            vpMain.setCurrentItem(1);
            ivPinglun.setSelected(true);
            ivCurrent = ivPinglun;
        } else if (position == 2 || position == R.id.ll_tab_faxian) {
            vpMain.setCurrentItem(2);
            ivFaxian.setSelected(true);
            ivCurrent = ivFaxian;
        } else if (position == 3 || position == R.id.ll_tab_me) {
            vpMain.setCurrentItem(3);
            ivMe.setSelected(true);
            ivCurrent = ivMe;
        }
    }

    @Override
    public void onClick(View v) {
        // 页面切换
        changeTab(v.getId());
    }
}